package com.peak.distribution.web.validator.method;

import java.util.Map;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Value;

import com.peak.distribution.web.validator.ValidateRuleMethod;
import com.peak.spring.boot.tools.Converts;
import com.peak.spring.boot.tools.Emptys;

public class CNString implements ValidateRuleMethod {

	@Value("${validate.rule.cn}")
	private String errorMessage;

	private String pattern = "^[\u0391-\uFFE5]*$";

	@Override
	public boolean execute(Object value) {
		if (Emptys.isEmpty(value)) {// 空的数据不校验，使用Require类校验
			return true;
		}
		Pattern p = Pattern.compile(pattern);
		return p.matcher(Converts.toString(value)).matches();
	}

	@Override
	public String getErrorMessageTemplate() {
		return Emptys.isEmpty(this.errorMessage) ? "{0}中包含不合法的字符，请输入正确的中文字符" : this.errorMessage;
	}

	@Override
	public void setParams(Map<String, Object> param) {
		Object pattern = param.get("pattern");
		if (Emptys.isNotEmpty(pattern)) {
			this.pattern = Converts.toString(pattern);
		}
	}

}
